package org.example.struct.array2;

import com.alibaba.fastjson.JSON;

/**
 * @author liyishan
 * @date 2023/10/9 10:15
 * @apiNote
 */

public class Test00289GameOfLife {
    public static void main(String[] args) {
        int[][] board = {{0, 1, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}};
        test(board);
        System.out.println(JSON.toJSON(board));
    }

    public static void test(int[][] board) {
        int[][] move = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
        int[][] copy = new int[board.length][board[0].length];
        for (int i = 0; i < board.length; i++) {
            System.arraycopy(board[i], 0, copy[i], 0, board[0].length);
        }
        int m = board.length;
        int n = board[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int count = 0;
                for (int k = 0; k < 8; k++) {
                    if (i + move[k][0] >= 0 && i + move[k][0] < m && j + move[k][1] >= 0 && j + move[k][1] < n && copy[i + move[k][0]][j + move[k][1]] == 1) {
                        count++;
                    }
                }
                if (copy[i][j] == 1 && (count < 2 || count > 3)) {
                    board[i][j] = 0;
                }
                if (copy[i][j] == 0 && count == 3) {
                    board[i][j] = 1;
                }
            }
        }
    }
}
